package com.wenyang.controller;

import com.wenyang.dao.UserDao;
import com.wenyang.entity.TabUser;
import com.wenyang.entity.VO.TabUserVo;
import com.wenyang.service.UserService;
import com.wenyang.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Zx
 * @Date 2024/3/29 8:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户管理",tags = {"用户管理"})
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;

    @ApiOperation("用户管理")
    @PostMapping("/listByPage")
    public Map<String,Object> listByPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam(required = false) String username){
        int startIdx = (pageNum - 1) * pageSize,size = pageSize;
        List<TabUserVo> userList = userService.listByPage(startIdx,size,username);
        int total = userDao.selectPageWithParam();
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",userList);
        map.put("total",total);
        map.put("msg","执行成功");
        return map;
    }


    @ApiOperation("添加用户信息")
    @PostMapping("/saveUser")
    public R saveUser(TabUser tabUser){
        TabUser tabUser1 = userService.findByUid(tabUser.getUid());
        if (null == tabUser1 || "".equals(tabUser1)) {
            return R.success(userService.saveUser(tabUser));
        }else{
            return R.success(userService.changeUser(tabUser));
        }
    }

    @ApiOperation("状态管理")
    @PostMapping("/updateStatus")
    public R updateStatus(@RequestParam Integer uid,String status){
        return R.success(userService.updateStatus(uid,status));
    }


    @ApiOperation("用户信息修改")
    @PutMapping("/changeUser")
    public R changeUser(TabUser tabUser){
        return R.success(userService.changeUser(tabUser));
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@RequestParam String username,@RequestParam String password){
        TabUserVo tabUser = userService.selectUsername(username);
        if (null == tabUser || "".equals(tabUser)) {
            return R.success("当前用户不存在！");
        }else{
            return R.success("登录成功！");
        }
    }

}
